package com.bw.publish

import android.content.Context
import android.opengl.GLSurfaceView
import android.os.Build
import android.os.Environment
import android.view.View
import androidx.annotation.RequiresApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_miv.BaseBox
import com.qiniu.pili.droid.shortvideo.PLAudioEncodeSetting
import com.qiniu.pili.droid.shortvideo.PLCameraSetting
import com.qiniu.pili.droid.shortvideo.PLFaceBeautySetting
import com.qiniu.pili.droid.shortvideo.PLMicrophoneSetting
import com.qiniu.pili.droid.shortvideo.PLRecordSetting
import com.qiniu.pili.droid.shortvideo.PLRecordStateListener
import com.qiniu.pili.droid.shortvideo.PLShortVideoRecorder
import com.qiniu.pili.droid.shortvideo.PLVideoEncodeSetting
import com.qiniu.pili.droid.shortvideo.PLVideoSaveListener
import java.io.File


@RequiresApi(Build.VERSION_CODES.R)
@Composable
fun PublishDetailPage(navHostController: NavHostController) {
    val mShortVideoRecorder = PLShortVideoRecorder()

    var state by remember {
        mutableStateOf(true)
    }

    var tex by remember {
        mutableStateOf("开拍")
    }

    Column(
        Modifier
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        Box(Modifier.fillMaxSize()) {
            AndroidView(modifier = Modifier.fillMaxSize(), factory = {
                QiNiu(it, mShortVideoRecorder)
            })

            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
                Text(text = tex, modifier = Modifier.clickable {
                    if (state) {
                        tex = "结束"
                        ToastUtils.showLong("开始录制")
                        // 开始一段视频
                        mShortVideoRecorder.beginSection()
                    } else {
                        tex = "开拍"
                        ToastUtils.showLong("结束录制")
                        // 结束一段视频
                        mShortVideoRecorder.endSection()
                        mShortVideoRecorder.concatSections(object : PLVideoSaveListener {
                            override fun onSaveVideoSuccess(p0: String?) {
                                ToastUtils.showLong("录制成功")
                            }

                            override fun onSaveVideoFailed(p0: Int) {
                                ToastUtils.showLong("录制失败")
                            }

                            override fun onSaveVideoCanceled() {

                            }

                            override fun onProgressUpdate(p0: Float) {

                            }

                        })
                    }
                    state = !state
                }, color = Color.White)
            }
        }
    }
}

@RequiresApi(Build.VERSION_CODES.R)
fun QiNiu(it: Context, mShortVideoRecorder: PLShortVideoRecorder): View {
    mShortVideoRecorder.setRecordStateListener(object : PLRecordStateListener {
        override fun onReady() {

        }

        override fun onError(p0: Int) {

        }

        override fun onDurationTooShort() {

        }

        override fun onRecordStarted() {

        }

        override fun onSectionRecording(p0: Long, p1: Long, p2: Int) {

        }

        override fun onRecordStopped() {

        }

        override fun onSectionIncreased(p0: Long, p1: Long, p2: Int) {

        }

        override fun onSectionDecreased(p0: Long, p1: Long, p2: Int) {

        }

        override fun onRecordCompleted() {

        }

        protected fun onResume() {
            mShortVideoRecorder.resume()
        }

        protected fun onPause() {
            mShortVideoRecorder.pause()
        }

        protected fun onDestroy() {
            mShortVideoRecorder.destroy()
        }
    })


    // 摄像头采集选项
    val cameraSetting = PLCameraSetting()
    cameraSetting.setCameraId(PLCameraSetting.CAMERA_FACING_ID.CAMERA_FACING_FRONT)
    cameraSetting.setCameraPreviewSizeRatio(PLCameraSetting.CAMERA_PREVIEW_SIZE_RATIO.RATIO_4_3)
    cameraSetting.setCameraPreviewSizeLevel(PLCameraSetting.CAMERA_PREVIEW_SIZE_LEVEL.PREVIEW_SIZE_LEVEL_480P)

    // 麦克风采集选项
    val microphoneSetting = PLMicrophoneSetting()

    // 视频编码选项
    val videoEncodeSetting = PLVideoEncodeSetting(it)
    videoEncodeSetting.setEncodingSizeLevel(PLVideoEncodeSetting.VIDEO_ENCODING_SIZE_LEVEL.VIDEO_ENCODING_SIZE_LEVEL_480P_1) // 480x480
    videoEncodeSetting.setEncodingBitrate(1000 * 1024) // 1000kbps
    videoEncodeSetting.setEncodingFps(25)
    videoEncodeSetting.setHWCodecEnabled(true) // true:硬编 false:软编

    // 音频编码选项
    val audioEncodeSetting = PLAudioEncodeSetting()
    audioEncodeSetting.setHWCodecEnabled(true) // true:硬编 false:软编

    // 美颜选项
    val faceBeautySetting = PLFaceBeautySetting(1.0f, 0.5f, 0.5f)

    //sd卡
    val path = Environment.getExternalStorageDirectory().absolutePath

    // 录制选项
    val recordSetting = PLRecordSetting()
    recordSetting.setMaxRecordDuration((10 * 1000).toLong()) // 10s
    recordSetting.setVideoCacheDir(path)
    recordSetting.setVideoFilepath(path + File.separator + "record.mp4")

    val gls = GLSurfaceView(it)
    mShortVideoRecorder.prepare(
        gls, cameraSetting, microphoneSetting,
        videoEncodeSetting, audioEncodeSetting, faceBeautySetting, recordSetting
    )

    return gls
}